<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form
          @submit.native.prevent
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="会员名称" prop="user">
            <el-input
              v-model="queryParams.user"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
          <el-form-item label="消费类型" prop="type">
            <el-select v-model="queryParams.type" placeholder="请选择">
              <el-option
                v-for="dict in recharge_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              >批量导出</el-button
            >
          </el-col>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList">
          <el-table-column label="序号" width="50" type="index" />
          <el-table-column label="会员名称" align="center" prop="user">
            <template #default="scope">
              {{ scope.row.user.name }}
            </template>
          </el-table-column>
          <el-table-column
            label="交易号"
            align="center"
            key="orderCode"
            prop="orderCode"
            width="120"
          />
          <el-table-column label="消费类型" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="recharge_type" :value="scope.row.type" />
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="170"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.gmtCreate) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="recharge_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="支付类型" align="center">
            <span> 微信支付</span>
          </el-table-column>
          <el-table-column
            label="总价"
            align="center"
            key="amount"
            prop="amount"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <a
                href="javascript:;"
                style="
                  color: #358de5;
                  text-decoration: none;
                  margin-right: 10px;
                "
                @click="handleUpdate(scope.row, 1)"
                >查看</a
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        @submit.native.prevent
        :model="form"
        ref="userRef"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员名称" prop="name">
              <el-input
                v-model="form.user.name"
                placeholder="请输入"
                maxlength="30"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易号" prop="name">
              <el-input
                v-model="form.orderCode"
                placeholder="请输入"
                maxlength="30"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消费类型">
              <el-radio-group v-model="form.type">
                <el-radio
                  v-for="dict in recharge_type"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易日期" prop="birthDate">
              <el-date-picker
                v-model="form.gmtCreate"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in recharge_status"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信支付交易ID" label-width="120" prop="transactionId">
              <el-input
                v-model="form.transactionId"
                placeholder="请输入"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="title !== '详情'">
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
    
    <script setup name="pay">
import { queryRecordPage, editRecord, recordExport } from "@/api/record";
const { proxy } = getCurrentInstance();
const { recharge_type, recharge_status } = proxy.useDict(
  "recharge_type",
  "recharge_status"
);

const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {
    id: undefined,
    user: undefined,
    status: undefined,
    type: undefined,
    amount: undefined,
    gmtCreate: undefined,
    orderCode: undefined,
    remark: undefined,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 1000,
    user: undefined,
    type: undefined,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询用户列表 */
const getList = () => {
  loading.value = true;
  queryRecordPage(proxy.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    loading.value = false;
    userList.value = res.rows;
    total.value = Number(res.total);
  });
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value = {
    pageNum: 1,
    pageSize: 1000,
    user: undefined,
    type: undefined,
  };
  handleQuery();
};

/** 导出按钮操作 */
const handleExport = async () => {
  proxy.$modal.loading("文件导出中，请稍候...");
  let result = await recordExport();
  const blob = new Blob([result]);
  let link = document.createElement("a");
  link.href = URL.createObjectURL(blob);
  link.download = "充值列表_" + new Date().getTime() + ".xlsx";
  link.click();
  proxy.$modal.closeLoading();
};

/** 重置操作表单 */
const reset = () => {
  form.value = {
    id: undefined,
    user: undefined,
    status: undefined,
    type: undefined,
    amount: undefined,
    gmtCreate: undefined,
    orderCode: undefined,
    remark: undefined,
  };
  proxy.resetForm("userRef");
};

/** 取消按钮 */
const cancel = () => {
  open.value = false;
  changeScore.value = false;
  reset();
};

/** 修改按钮操作 */
const handleUpdate = (row, type) => {
  reset();
  title.value = type == 1 ? "详情" : "编辑";
  form.value = {
    ...row,
  };
  open.value = true;
};

/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["userRef"].validate((valid) => {
    if (valid) {
      editRecord(form.value).then((response) => {
        proxy.$modal.msgSuccess("编辑成功");
        open.value = false;
        getList();
      });
    }
  });
};

getList();
</script>
    <style lang='scss' scoped>
</style>
    